home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / tests / stdio / stdio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-12  |  43.3 KB  |  1,656 lines

  1. /* 
  2.  * stdio.c --
  3.  *
  4.  *    This file contains a program that exercises the stdio
  5.  *    library facilities.  Invoke it with no parameters;  it
  6.  *    will print messages on stderr for any problems it detects
  7.  *    with the stdio procedures.
  8.  *
  9.  * Copyright 1988 Regents of the University of California
  10.  * Permission to use, copy, modify, and distribute this
  11.  * software and its documentation for any purpose and without
  12.  * fee is hereby granted, provided that the above copyright
  13.  * notice appear in all copies.  The University of California
  14.  * makes no representations about the suitability of this
  15.  * software for any purpose.  It is provided "as is" without
  16.  * express or implied warranty.
  17.  */
  18.  
  19. #ifndef lint
  20. static char rcsid[] = "$Header: /sprite/src/tests/stdio/RCS/stdio.c,v 1.12 91/12/12 13:43:34 shirriff Exp $ SPRITE (Berkeley)";
  21. #endif not lint
  22.  
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <sys/file.h>
  26. #include <sys/wait.h>
  27. #include <signal.h>
  28. #include <varargs.h>
  29.  
  30. #ifdef sun3
  31. #define BIGENDIAN
  32. #endif
  33. #ifdef sun4
  34. #define BIGENDIAN
  35. #endif
  36. #ifdef ds3100
  37. #define LITTLEENDIAN
  38. #endif
  39.  
  40. #if 1
  41. #define error(string) \
  42.     fprintf(stderr, string); \
  43.     exit(1);
  44. #else
  45. #define error(string) \
  46.     fprintf(stderr, string);
  47. #endif
  48.  
  49. /*
  50.  *----------------------------------------------------------------------
  51.  *
  52.  * CheckFile --
  53.  *
  54.  *    Utility procedure to make sure that a given file contains a
  55.  *    given value.  Aborts with error if it doesn't.
  56.  *
  57.  * Results:
  58.  *    None.
  59.  *
  60.  * Side effects:
  61.  *    None.
  62.  *
  63.  *----------------------------------------------------------------------
  64.  */
  65.  
  66. void
  67. CheckFile(name, value, errMsg)
  68.     char *name;            /* Name of file to read. */
  69.     char *value;        /* Should match contents of file. */
  70.     char *errMsg;        /* Error message if there's a mismatch. */
  71. {
  72.     char buf[BUFSIZ];
  73.     int count, id;
  74.  
  75.     id = open(name, O_RDONLY, 0);
  76.     if (id < 0) {
  77.     error(errMsg);
  78.     }
  79.     count = read(id, buf, 1000);
  80.     if ((count == -1) || (count == 1000)) {
  81.     error(errMsg);
  82.     }
  83.     buf[count] = 0;
  84.     if (strcmp(buf, value) != 0) {
  85.     error(errMsg);
  86.     }
  87.     close(id);
  88. }
  89.  
  90. /*
  91.  *----------------------------------------------------------------------
  92.  *
  93.  * TsCheck --
  94.  *
  95.  *    Procedure for comparing two special-purpose structures, for
  96.  *    testing fread and fwrite.
  97.  *
  98.  * Results:
  99.  *    None.
  100.  *
  101.  * Side effects:
  102.  *    Aborts if ts1 and ts2 aren't equal.
  103.  *
  104.  *----------------------------------------------------------------------
  105.  */
  106.  
  107. typedef struct {
  108.     int x;
  109.     char c[6];
  110.     short y;
  111. } testStruct;
  112.  
  113. void
  114. TsCheck(ts1, ts2, msg)
  115.     testStruct *ts1, *ts2;        /* Two structures to compare for
  116.                      * equality. */
  117.     char *msg;                /* Message to print if mismatch. */
  118. {
  119.     int i;
  120.     if ((ts1->x != ts2->x) || (ts1->y != ts2->y)) {
  121.     error(msg);
  122.     }
  123.     for (i = 0; i < 6; i++) {
  124.     if (ts1->c[i] != ts2->c[i]) {
  125.         error(msg);
  126.     }
  127.     }
  128. }
  129.  
  130. /*
  131.  *----------------------------------------------------------------------
  132.  *
  133.  * CheckNext --
  134.  *
  135.  *    Procedure for testing fprintf:  reads back from a file and
  136.  *    makes sure what was printed had the right length and content..
  137.  *
  138.  * Results:
  139.  *    None.
  140.  *
  141.  * Side effects:
  142.  *    Aborts if the contents of the file's next line don't match string
  143.  *    and length.
  144.  *
  145.  *----------------------------------------------------------------------
  146.  */
  147.  
  148. void
  149. CheckNext(f, string, length, msg)
  150.     FILE *f;            /* File from which to read a line. */
  151.     char *string;        /* This should match exactly the line. */
  152.     int length;            /* The line should be this long. */
  153.     char *msg;            /* Error message to print if there's
  154.                  * a problem. */
  155. {
  156.     char buffer[500];
  157.     if (fgets(buffer, 500, f) == NULL) {
  158.     error(msg);
  159.     }
  160.     if (strlen(buffer) != length) {
  161.     error(msg);
  162.     }
  163.     if (strcmp(buffer, string) != 0) {
  164.     error(msg);
  165.     }
  166. }
  167.  
  168. /*
  169.  *----------------------------------------------------------------------
  170.  *
  171.  * CheckFp --
  172.  *
  173.  *    This procedure is used to check floating-point scanning.  It
  174.  *    prints the numbers, so as to do an approximate check, rather than
  175.  *    an exact one (fp conversion isn't exact, after all)..
  176.  *
  177.  * Results:
  178.  *    None.
  179.  *
  180.  * Side effects:
  181.  *    Aborts if the printed version of f1-f4 don't match string.
  182.  *
  183.  *----------------------------------------------------------------------
  184.  */
  185.  
  186. void
  187. CheckFp(count1, count2, f1, f2, f3, f4, string, msg)
  188.     int count1, count2;        /* These two counts better match. */
  189.     double f1, f2, f3, f4;    /* Four numbers; when printed in %e format,
  190.                  * they better match string. */
  191.     char *string;        /* Desired values of numbers. */
  192.     char *msg;            /* Error message. */
  193. {
  194.     char printed[200];
  195.  
  196.     if (count1 != count2) {
  197.     error(msg);
  198.     }
  199.     sprintf(printed, "%e %e %e %e", f1, f2, f3, f4);
  200.     if (strcmp(printed, string) != 0) {
  201.     printf("CheckFP: %s vs. %s\n", printed, string);
  202.     error(msg);
  203.     }
  204. }
  205.  
  206. /*
  207.  *----------------------------------------------------------------------
  208.  *
  209.  * CheckString --
  210.  *
  211.  *    Utility procedure for checking sscanf on strings.  Scans input
  212.  *    according to format, into four result strings, and compares them
  213.  *    against s1-s4.
  214.  *
  215.  * Results:
  216.  *    None.
  217.  *
  218.  * Side effects:
  219.  *    Aborts if there's a mismatch.
  220.  *
  221.  *----------------------------------------------------------------------
  222.  */
  223.  
  224.  
  225. void
  226. CheckString(input, format, count, s1, s2, s3, s4, msg)
  227.     char *format;        /* Format to use for scanning. */
  228.     char *input;        /* Input string to scan. */
  229.     int count;            /* Expected return value from sscanf. */
  230.     char *s1, *s2, *s3, *s4;    /* Scanned strings should match this. */
  231.     char *msg;            /* Message to print on error. */
  232. {
  233.     char c1[100], c2[100], c3[100], c4[100];
  234.     int result, i;
  235.  
  236.     for (i = 0; i < 100; i++) {
  237.     char c;
  238.     if (i < 5) {
  239.         c = 'X';
  240.     } else {
  241.         c = 0;
  242.     }
  243.     c1[i] = c2[i] = c3[i] = c4[i] = c;
  244.     }
  245.     result = sscanf(input, format, c1, c2, c3, c4);
  246.     if ((result != count) || (strcmp(c1, s1) != 0) || (strcmp(c2, s2) != 0)
  247.         || (strcmp(c3, s3) != 0) || (strcmp(c4, s4) != 0)) {
  248.     error(msg);
  249.     }
  250. }
  251.  
  252. /*
  253.  *----------------------------------------------------------------------
  254.  *
  255.  * CheckVprintf --
  256.  *
  257.  *    This procedure is used to check vprintf:  it just packages its
  258.  *    arguments and calls vprintf.
  259.  *
  260.  * Results:
  261.  *    Whatever vprintf returns.
  262.  *
  263.  * Side effects:
  264.  *    None.
  265.  *
  266.  *----------------------------------------------------------------------
  267.  */
  268.  
  269. int
  270. CheckVprintf(va_alist)
  271.     va_dcl        /* Format string followed by one or more arguments
  272.              * to printf. */
  273. {
  274.     char *format;
  275.     va_list args;
  276.  
  277.     va_start(args);
  278.     format = va_arg(args, char *);
  279.     return vprintf(format, args);
  280. }
  281.  
  282. /*
  283.  *----------------------------------------------------------------------
  284.  *
  285.  * CheckVsprintf --
  286.  *
  287.  *    This procedure is used to check vsprintf:  it just packages its
  288.  *    arguments and calls vsprintf.
  289.  *
  290.  * Results:
  291.  *    Whatever vsprintf returns.
  292.  *
  293.  * Side effects:
  294.  *    None.
  295.  *
  296.  *----------------------------------------------------------------------
  297.  */
  298.  
  299. char *
  300. CheckVsprintf(va_alist)
  301.     va_dcl        /* Format string followed by one or more arguments
  302.              * to printf. */
  303. {
  304.     char *string;
  305.     char *format;
  306.     va_list args;
  307.  
  308.     va_start(args);
  309.     string = va_arg(args, char *);
  310.     format = va_arg(args, char *);
  311.     return vsprintf(string, format, args);
  312. }
  313.  
  314. /*
  315.  *----------------------------------------------------------------------
  316.  *
  317.  * main --
  318.  *
  319.  *    Main program for stdio testing.
  320.  *
  321.  * Results:
  322.  *    None.
  323.  *
  324.  * Side effects:
  325.  *    Runs a whole bunch of little tests on the stdio procedures,
  326.  *    and aborts with an error message if any unexpected behavior
  327.  *    is observed.  Exits with 0 status and no output if all goes
  328.  *    well.
  329.  *
  330.  *----------------------------------------------------------------------
  331.  */
  332.  
  333. main()
  334. {
  335.     FILE *f1, *f2, *f3;
  336.     char buf[BUFSIZ], c;
  337.     int i, fd, fd2, pid, length, count;
  338.     testStruct tsArray1[20], tsArray2[20]; 
  339.     union wait status;
  340.     int d1, d2, d3, d4;
  341.     float fp1, fp2, fp3, fp4;
  342.     double dbl1, dbl2, dbl3, dbl4;
  343.     short s1, s2, s3, sa1[4], sa2[4];
  344.  
  345.     /*
  346.      * putc, fputc, fputs
  347.      */
  348.  
  349.     f1 = fopen("test", "w");
  350.     if (f1 == NULL) {
  351.     error("putc error 1\n");
  352.     }
  353.     if (putc('a', f1) == EOF) {
  354.     error("putc error 2\n");
  355.     }
  356.     if (putc('b', f1) == EOF) {
  357.     error("putc error 3\n");
  358.     }
  359.     if (fputc('c', f1) != 'c') {
  360.     error("putc error 4\n");
  361.     }
  362.     if (fputc('\n', f1) != '\n') {
  363.     error("putc error 5\n");
  364.     }
  365.     if (fputs("This is the second line.\n", f1) == EOF) {
  366.     error("putc error 6\n");
  367.     }
  368.     i = fclose(f1);
  369.     if (i != 0) {
  370.     error("putc error 7\n");
  371.     }
  372.     CheckFile("test", "abc\nThis is the second line.\n", "putc error 8\n");
  373.     f1 = fopen("test2", "w");
  374.     if (fputc(0377, f1) != 0377) {
  375.     error("putc error 9\n");
  376.     }
  377.     if (putc(0377, f1) != 0377) {
  378.     error("putc error 10\n");
  379.     }
  380.     if (fputc(-1, f1) != 0377) {
  381.     error("putc error 11\n");
  382.     }
  383.     if (putc(-1, f1) != 0377) {
  384.     error("putc error 12\n");
  385.     }
  386.     fclose(f1);
  387.     f1 = fopen("test", "r");
  388.     if (putc('a', f1) != EOF) {
  389.     error("putc error 13\n");
  390.     }
  391.     if (fputc('a', f1) != EOF) {
  392.     error("putc error 14\n");
  393.     }
  394.     if (fputs("abcde", f1) != EOF) {
  395.     error("putc error 15\n");
  396.     }
  397.  
  398.     /*
  399.      * getc, fgetc, fgets
  400.      */
  401.  
  402.     c = getc(f1);
  403.     if (c != 'a') {
  404.     error("getc error 1\n");
  405.     }
  406.     buf[4] = 'Z';
  407.     if (fgets(buf, 100, f1) == NULL) {
  408.     error("getc error 2\n");
  409.     }
  410.     if (strcmp(buf, "bc\n") != 0) {
  411.     error("getc error 3\n");
  412.     }
  413.     if (buf[4] != 'Z') {
  414.     error("getc error 4\n");
  415.     }
  416.     c = fgetc(f1);
  417.     if (c != 'T') {
  418.     error("getc error 5\n");
  419.     }
  420.     buf[10] = 'X';
  421.     if (fgets(buf, 10, f1) == NULL) {
  422.     error("getc error 6\n");
  423.     }
  424.     if (strcmp(buf, "his is th") != 0) {
  425.     error("getc error 7\n");
  426.     }
  427.     if (buf[10] != 'X') {
  428.     error("getc error 8\n");
  429.     }
  430.     buf[16] = 0377;
  431.     if (fgets(buf, 100, f1) == NULL) {
  432.     error("getc error 9\n");
  433.     }
  434.     if (strcmp(buf, "e second line.\n") != 0) {
  435.     error("getc error 8\n");
  436.     }
  437.     if (buf[16] != -1) {
  438.     error("getc error 9\n");
  439.     }
  440.     if (getc(f1) != EOF) {
  441.     error("getc error 10\n");
  442.     }
  443.     if (fgetc(f1) != EOF) {
  444.     error("getc error 11\n");
  445.     }
  446.     if (fgets(buf, 1000, f1) != NULL) {
  447.     error("getc error 12\n");
  448.     }
  449.     i = fclose(f1);
  450.     if (i != 0) {
  451.     error("getc error 13\n");
  452.     }
  453.     f1 = fopen("test", "w");
  454.     if (f1 == NULL) {
  455.     error("getc error 14\n");
  456.     }
  457.     if (getc(f1) != EOF) {
  458.     error("getc error 15\n");
  459.     }
  460.     fputs("Line with no return", f1);
  461.     fclose(f1);
  462.     f1 = fopen("test", "r");
  463.     if (f1 == NULL) {
  464.     error("getc error 16\n");
  465.     }
  466.     if (fgets(buf, 1000, f1) != NULL) {
  467.     error("getc error 17\n");
  468.     }
  469.     fclose(f1);
  470.  
  471.     /*
  472.      * putw, getw
  473.      */
  474.  
  475.     f1 = fopen("test", "w");
  476.     if (f1 == NULL) {
  477.     error("putw error 1\n");
  478.     }
  479.     putc('a', f1);
  480.     if (putw(0x010203ff, f1) != 0) {
  481.     error("putw error 2\n");
  482.     }
  483.     if (putw(0x04050607, f1) != 0) {
  484.     error("putw error 3\n");
  485.     }
  486.     if (getw(f1) != EOF) {
  487.     error("putw error 4\n");
  488.     }
  489.     fclose(f1);
  490. #ifndef LITTLEENDIAN
  491.     CheckFile("test", "a\1\2\3\377\4\5\6\7", "putw error 5(B)\n");
  492. #endif
  493. #ifndef BIGENDIAN
  494.     CheckFile("test", "a\377\3\2\1\7\6\5\4", "putw error 5(L)\n");
  495. #endif
  496.     f1 = fopen("test", "r");
  497.     if (f1 == NULL) {
  498.     error("putw error 6\n");
  499.     }
  500.     (void) getc(f1);
  501.     d1 = getw(f1);
  502.     if (feof(f1)) {
  503.     error("putw error 7\n");
  504.     }
  505.     d2 = getw(f1);
  506.     if (feof(f1)) {
  507.     error("putw error 8\n");
  508.     }
  509.     d3 = getw(f1);
  510.     if (!feof(f1)) {
  511.     error("putw error 9\n");
  512.     }
  513.     if ((d1 != 0x010203ff) || (d2 != 0x04050607) || (d3 != EOF)) {
  514.     error("putw error 10\n");
  515.     }
  516.     if (putw(1, f1) != EOF) {
  517.     error("putw error 11\n");
  518.     }
  519.     fclose(f1);
  520.  
  521.     /*
  522.      * fileno
  523.      */
  524.  
  525.     if (fileno(stdin) != 0) {
  526.     error("fileno error 1\n");
  527.     }
  528.     if (fileno(stdout) != 1) {
  529.     error("fileno error 2\n");
  530.     }
  531.     if (fileno(stderr) != 2) {
  532.     error("fileno error 3\n");
  533.     }
  534.     fd = dup(1);
  535.     fclose(stdout);
  536.     f1 = fopen("test", "w");
  537.     if (fileno(f1) != 1) {
  538.     error("fileno error 4\n");
  539.     }
  540.  
  541.     /*
  542.      * putchar, puts
  543.      */
  544.  
  545.     if (putchar('x') == EOF) {
  546.     error("putchar error 1\n");
  547.     }
  548.     if (puts(" more on this line.") == EOF) {
  549.     error("putchar error 2\n");
  550.     }
  551.     if (puts("A second line.") == EOF) {
  552.     error("putchar error 3\n");
  553.     }
  554.     fclose(f1);
  555.     if (putchar('x') != EOF) {
  556.     error("putchar error 4\n");
  557.     }
  558.     if (puts("More junk.") != EOF) {
  559.     error("putchar error 5\n");
  560.     }
  561.     dup2(fd, 1);
  562.     (void) fdopen(1, "w");
  563.     CheckFile("test", "x more on this line.\nA second line.\n",
  564.         "putchar error 6\n");
  565.     close(fd);
  566.  
  567.     /*
  568.      * getchar, gets
  569.      */
  570.  
  571.     fd = dup(0);
  572.     fclose(stdin);
  573.     f1 = fopen("test", "r");
  574.     if (fileno(f1) != 0) {
  575.     error("getchar error 1\n");
  576.     }
  577.     if (getchar() != 'x') {
  578.     error("getchar error 2\n");
  579.     }
  580.     if (getchar() != ' ') {
  581.     error("getchar error 3\n");
  582.     }
  583.     if (gets(buf) != buf) {
  584.     error("gets error 1\n");
  585.     }
  586.     if (strcmp(buf, "more on this line.") != 0) {
  587.     error("gets error 2\n");
  588.     }
  589.     if (gets(buf) != buf) {
  590.     error("gets error 3\n");
  591.     }
  592.     if (strcmp(buf, "A second line.") != 0) {
  593.     error("gets error 4\n");
  594.     }
  595.     if (gets(buf) != NULL) {
  596.     error("gets error 5\n");
  597.     }
  598.     if (getchar() != EOF) {
  599.     error("getchar error 4\n");
  600.     }
  601.     dup2(fd, 0);
  602.     (void) fdopen(0, "r");
  603.     close(fd);
  604.  
  605.     /*
  606.      * fwrite, fread
  607.      */
  608.  
  609.     for (i = 0; i < 10; i++) {
  610.     int j;
  611.  
  612.     tsArray1[i].x = 0x52ff6984 + i;
  613.     tsArray1[i].y = 47+i;
  614.     for (j = 0; j < 6; j++) {
  615.         tsArray1[i].c[j] = 'A' + 2*i + j;
  616.     }
  617.     }
  618.     f1 = fopen("test", "w");
  619.     f2 = fopen("test", "r");
  620.     if (f1 == NULL) {
  621.     error("fwrite error 1\n");
  622.     }
  623.     if (f2 == NULL) {
  624.     error("fread error 1\n");
  625.     }
  626.     if (fwrite(tsArray1, sizeof(testStruct), 10, f1) != 10) {
  627.     error("fwrite error 2\n");
  628.     }
  629.     if (fwrite(&tsArray1[3], sizeof(testStruct), 5, f1) != 5) {
  630.     error("fwrite error 3\n");
  631.     }
  632.     if (fwrite(tsArray1, sizeof(testStruct), 1, f1) != 1) {
  633.     error("fwrite error 4\n");
  634.     }
  635.     if (fwrite(tsArray1, sizeof(testStruct), 1, f2) != 0) {
  636.     error("fwrite error 5\n");
  637.     }
  638.     fclose(f1);
  639.     if (fread(tsArray2, sizeof(testStruct), 2, f2) != 2) {
  640.     error("fread error 2\n");
  641.     }
  642.     TsCheck(&tsArray1[0], &tsArray2[0], "fread error 3\n");
  643.     TsCheck(&tsArray1[1], &tsArray2[1], "fread error 4\n");
  644.     if (fread(tsArray2, sizeof(testStruct), 10, f2) != 10) {
  645.     error("fread error 5\n");
  646.     }
  647.     for (i = 0; i < 10; i++) {
  648.     if (i >= 8) {
  649.         TsCheck(&tsArray1[i-5], &tsArray2[i], "fread error 6\n");
  650.     } else {
  651.         TsCheck(&tsArray1[i+2], &tsArray2[i], "fread error 7\n");
  652.     }
  653.     }
  654.     if (fread(tsArray2, sizeof(testStruct), 1, f2) != 1) {
  655.     error("fread error 8\n");
  656.     }
  657.     TsCheck(&tsArray1[5], &tsArray2[0], "fread error 9\n");
  658.     if (fread(tsArray2, sizeof(testStruct), 5, f2) != 3) {
  659.     error("fread error 10\n");
  660.     }
  661.     TsCheck(&tsArray1[6], &tsArray2[0], "fread error 11\n");
  662.     TsCheck(&tsArray1[7], &tsArray2[1], "fread error 12\n");
  663.     TsCheck(&tsArray1[0], &tsArray2[2], "fread error 13\n");
  664.     if (fread(tsArray2, sizeof(testStruct), 1, f2) != 0) {
  665.     error("fread error 14\n");
  666.     }
  667.     fclose(f2);
  668.  
  669.     /*
  670.      * fseek, rewind, and ftell
  671.      */
  672.  
  673.     f1 = fopen("test", "w+");
  674.     if (f1 == NULL) {
  675.     error("fseek error 1\n");
  676.     }
  677.     fputs("abcdefghijklmnopqrstuvwxyz", f1);
  678.     if (ftell(f1) != 26) {
  679.     error("fseek error 2\n");
  680.     }
  681.     fseek(f1, 2, SEEK_SET);
  682.     putc('1', f1);
  683.     if (ftell(f1) != 3) {
  684.     error("fseek error 3\n");
  685.     }
  686.     fseek(f1, 4, SEEK_CUR);
  687.     putc('2', f1);
  688.     if (ftell(f1) != 8) {
  689.     error("fseek error 4\n");
  690.     }
  691.     fseek(f1, -1, SEEK_END);
  692.     fputs("345", f1);
  693.     if (ftell(f1) != 28) {
  694.     error("fseek error 5\n");
  695.     }
  696.     fflush(f1);
  697.     CheckFile("test", "ab1defg2ijklmnopqrstuvwxy345", "fseek error 6\n");
  698.     fseek(f1, 1, SEEK_SET);
  699.     if (getc(f1) != 'b') {
  700.     error("fseek error 7\n");
  701.     }
  702.     if (ftell(f1) != 2) {
  703.     error("fseek error 8\n");
  704.     }
  705.     fseek(f1, -2, SEEK_END);
  706.     if (ftell(f1) != 26) {
  707.     error("fseek error 9\n");
  708.     }
  709.     if (getc(f1) != '4') {
  710.     error("fseek error 10\n");
  711.     }
  712.     if (getc(f1) != '5') {
  713.     error("fseek error 11\n");
  714.     }
  715.     if (getc(f1) != EOF) {
  716.     error("fseek error 12\n");
  717.     }
  718.     if (fseek(f1, -1, SEEK_CUR) != 0) {
  719.     error("fseek error 13\n");
  720.     }
  721.     if (getc(f1) != '5') {
  722.     error("fseek error 14\n");
  723.     }
  724.     rewind(f1);
  725.     if (getc(f1) != 'a') {
  726.     error("fseek error 15\n");
  727.     }
  728.     fclose(f1);
  729.     /*
  730.      * Check fseeks across buffer boundaries and with both read-only
  731.      * streams and read-write streams.
  732.      */
  733.     f1 = fopen("test", "w");
  734.     if (f1 == NULL) {
  735.     error("fseek error 16\n");
  736.     }
  737.     for (i = 0; i <= 3 * BUFSIZ - 1; i++) {
  738.     putc(i&0xff, f1);
  739.     }
  740.     fclose(f1);
  741.     f1 = fopen("test", "r");
  742.     if (f1 == NULL) {
  743.     error("fseek error 16a\n");
  744.     }
  745.     if (getc(f1) != 0) {
  746.     error("fseek error 17\n");
  747.     }
  748.     fseek(f1, 0, SEEK_SET);
  749.     if (getc(f1) != 0) {
  750.     error("fseek error 17a\n");
  751.     }
  752.     fseek(f1, BUFSIZ-1, SEEK_SET);
  753.     if (getc(f1) != ((BUFSIZ-1) & 0xff)) {
  754.     error("fseek error 18\n");
  755.     }
  756.     fseek(f1, BUFSIZ+1, SEEK_SET);
  757.     if (getc(f1) != ((BUFSIZ+1) & 0xff)) {
  758.     error("fseek error 19\n");
  759.     }
  760.     if (getc(f1) != ((BUFSIZ+2) & 0xff)) {
  761.     error("fseek error 20\n");
  762.     }
  763.     fseek(f1, 3 * BUFSIZ - 2, SEEK_SET);
  764.     if (getc(f1) != ((3 * BUFSIZ - 2) & 0xff)) {
  765.     error("fseek error 21\n");
  766.     }
  767.     if (getc(f1) != ((3 * BUFSIZ - 1) & 0xff)) {
  768.     error("fseek error 22\n");
  769.     }
  770.     if (getc(f1) != EOF) {
  771.     error("fseek error 23\n");
  772.     }
  773.     fclose(f1);
  774.     f1 = fopen("test", "r+");
  775.     if (f1 == NULL) {
  776.     error("fseek error 24\n");
  777.     }
  778.     for (i = 0; i < 3 * BUFSIZ - 1; i++) {
  779.     putc(i&0xff, f1);
  780.     }
  781.     fseek(f1, 0, SEEK_SET);
  782.     if (getc(f1) != 0) {
  783.     error("fseek error 25\n");
  784.     }
  785.     fseek(f1, BUFSIZ-1, SEEK_SET);
  786.     if (getc(f1) != ((BUFSIZ-1) & 0xff)) {
  787.     error("fseek error 26\n");
  788.     }
  789.     fseek(f1, BUFSIZ+1, SEEK_SET);
  790.     if (getc(f1) != ((BUFSIZ+1) & 0xff)) {
  791.     error("fseek error 27\n");
  792.     }
  793.     if (getc(f1) != ((BUFSIZ+2) & 0xff)) {
  794.     error("fseek error 28\n");
  795.     }
  796.     fseek(f1, 3 * BUFSIZ - 2, SEEK_SET);
  797.     if (getc(f1) != ((3 * BUFSIZ - 2) & 0xff)) {
  798.     error("fseek error 29\n");
  799.     }
  800.     if (getc(f1) != ((3 * BUFSIZ - 1) & 0xff)) {
  801.     error("fseek error 30\n");
  802.     }
  803.     if (getc(f1) != EOF) {
  804.     error("fseek error 31\n");
  805.     }
  806.     fclose(f1);
  807.     
  808.     
  809.  
  810.     /*
  811.      * Buffering: setbuf, setbuffer, setlinebuf, setvbuf, fflush
  812.      */
  813.  
  814.     f1 = fopen("test", "w");
  815.     if (f1 == NULL) {
  816.     error("setvbuf error 1\n");
  817.     }
  818.     if (setvbuf(f1, buf, _IOFBF, 10) != 0) {
  819.     error("setvbuf error 2\n");
  820.     }
  821.     fputs("abcd\nefgh", f1);
  822.     if (strncmp(buf, "abcd\nefgh", 9) != 0) {
  823.     error("setvbuf error 3\n");
  824.     }
  825.     CheckFile("test", "", "setvbuf error 4\n");
  826.     putc('i', f1);
  827.     CheckFile("test", "abcd\nefghi", "setvbuf error 5\n");
  828.     fseek(f1, 0, SEEK_SET);
  829.     if (setvbuf(f1, 0, _IOLBF, 100) != 0) {
  830.     error("setvbuf error 6\n");
  831.     }
  832.     buf[0] = buf[1] = 0;
  833.     fputs("123456\n7890", f1);
  834.     if (buf[0] != 0) {
  835.     error("setvbuf error 7\n");
  836.     }
  837.     CheckFile("test", "123456\nghi", "setvbuf error 8\n");
  838.     if (fflush(f1) != 0) {
  839.     error("setvbuf error 9\n");
  840.     }
  841.     CheckFile("test", "123456\n7890", "setvbuf error 10\n");
  842.     putc('X', f1);
  843.     CheckFile("test", "123456\n7890", "setvbuf error 11\n");
  844.     if (setvbuf(f1, 0, _IONBF, BUFSIZ) != 0) {
  845.     error("setvbuf error 11\n");
  846.     }
  847.     CheckFile("test", "123456\n7890X", "setvbuf error 12\n");
  848.     putc('Y', f1);
  849.     CheckFile("test", "123456\n7890XY", "setvbuf error 13\n");
  850.     if (setvbuf(f1, 0, _IOFBF, 1) != 0) {
  851.     error("setvbuf error 14\n");
  852.     }
  853.     putc('Z', f1);
  854.     CheckFile("test", "123456\n7890XYZ", "setvbuf error 15\n");
  855.     setbuf(f1, buf);
  856.     for (i = 0; i < BUFSIZ-1; i++) {
  857.     putc(i&0xff, f1);
  858.     }
  859.     if ((buf[0] != 0) || ((buf[BUFSIZ-2] & 0xff) != ((BUFSIZ-2)&0xff))) {
  860.     error("setbuf error 1\n");
  861.     }
  862.     CheckFile("test", "123456\n7890XYZ", "setbuf error 2\n");
  863.     f2 = fopen("test", "r");
  864.     fseek(f2, 14, SEEK_SET);
  865.     if (getc(f2) != EOF) {
  866.     error("setbuf error 2\n");
  867.     }
  868.     putc('a', f1);
  869.     clearerr(f2);
  870.     c = getc(f2);
  871.     if (c != 0) {
  872.     error("setbuf error 3\n");
  873.     }
  874.     fseek(f2, BUFSIZ-3, SEEK_CUR);
  875.     c = getc(f2);
  876.     if ((c&0xff) != ((BUFSIZ-2)&0xff)) {
  877.     error("setbuf error 4\n");
  878.     }
  879.     if (getc(f2) != 'a') {
  880.     error("setbuf error 5\n");
  881.     }
  882.     if (getc(f2) != EOF) {
  883.     error("setbuf error 6\n");
  884.     }
  885.     fseek(f1, 0, SEEK_SET);
  886.     clearerr(f2);
  887.     fseek(f2, 0, SEEK_SET);
  888.     setlinebuf(f1);
  889.     putc('a', f1);
  890.     c = getc(f2);
  891.     if (c != '1') {
  892.     error("setlinebuf error 1\n");
  893.     }
  894.     putc('\n', f1);
  895.     f2 = freopen("test", "r", f2);
  896.     if (getc(f2) != 'a') {
  897.     error("setlinebuf error 2\n");
  898.     }
  899.     if (getc(f2) != '\n') {
  900.     error("setlinebuf error 3\n");
  901.     }
  902.     fclose(f2);
  903.     fclose(f1);
  904.     f1 = fopen("test", "w");
  905.     if (f1 == NULL) {
  906.     error("setbuffer error 1\n");
  907.     }
  908.     setbuffer(f1, buf, 5);
  909.     fputs("ABCD", f1);
  910.     if (strncmp(buf, "ABCD", 4) != 0) {
  911.     error("setbuffer error 2\n");
  912.     }
  913.     CheckFile("test", "", "setbuffer error 3\n");
  914.     putc('E', f1);
  915.     CheckFile("test", "ABCDE", "setbuffer error 4\n");
  916.     setbuffer(f1, 0, 1000);
  917.     putc('F', f1);
  918.     CheckFile("test", "ABCDEF", "setbuffer error 5\n");
  919.     fclose(f1);
  920.     fclose(f2);
  921.  
  922.     /*
  923.      * ungetc
  924.      */
  925.  
  926.     f1 = fopen("test", "r");
  927.     (void) getc(f1);
  928.     (void) getc(f1);
  929.     if (ungetc('x', f1) != 'x') {
  930.     error("ungetc error 1\n");
  931.     }
  932.     if (ungetc('y', f1) != 'y') {
  933.     error("ungetc error 2\n");
  934.     }
  935.     if (ungetc('a', f1) != EOF) {
  936.     error("ungetc error 3\n");
  937.     }
  938.     fgets(buf, 100, f1);
  939.     if (getc(f1) != EOF) {
  940.     error("ungetc error 4\n");
  941.     }
  942.     if (ungetc('#', f1) != '#') {
  943.     error("ungetc error 5\n");
  944.     }
  945.     if (ungetc(EOF, f1) != EOF) {
  946.     error("ungetc error 6\n");
  947.     }
  948.     if (getc(f1) != '#') {
  949.     error("ungetc error 7\n");
  950.     }
  951.     if (getc(f1) != EOF) {
  952.     error("ungetc error 8\n");
  953.     }
  954.  
  955.     /*
  956.      * fopen
  957.      */
  958.  
  959.     f1 = fopen("test", "w");
  960.     if (f1 == NULL) {
  961.     error("fopen error 1\n");
  962.     }
  963.     CheckFile("test", "", "fopen error 2\n");
  964.     fputs("Test output", f1);
  965.     fseek(f1, 0, SEEK_SET);
  966.     if (getc(f1) != EOF) {
  967.     error("fopen error 3\n");
  968.     }
  969.     fclose(f1);
  970.     f1 = fopen("test", "r");
  971.     if (f1 == NULL) {
  972.     error("fopen error 4\n");
  973.     }
  974.     if (getc(f1) != 'T') {
  975.     error("fopen error 5\n");
  976.     }
  977.     if (putc('x', f1) != EOF) {
  978.     error("fopen error 6\n");
  979.     }
  980.     fclose(f1);
  981.     f1 = fopen("test", "a");
  982.     if (f1 == NULL) {
  983.     error("fopen error 7\n");
  984.     }
  985.     fputs(" continued", f1);
  986.     fseek(f1, 0, SEEK_SET);
  987.     fputs(" again", f1);
  988.     fclose(f1);
  989.     CheckFile("test", " againutput continued", "fopen error 8\n");
  990.     f1= fopen("test", "w");
  991.     fputs("Test output continued again", f1);
  992.     fclose(f1);
  993.     f1= fopen("test", "rb+");
  994.     if (f1 == NULL) {
  995.     error("fopen error 9\n");
  996.     }
  997.     if (getc(f1) != 'T') {
  998.     error("fopen error 10\n");
  999.     }
  1000.     fseek(f1, 0, SEEK_CUR);
  1001.     if (fputs("urd", f1) == EOF) {
  1002.     error("fopen error 11\n");
  1003.     }
  1004.     fclose(f1);
  1005.     CheckFile("test", "Turd output continued again", "fopen error 12\n");
  1006.     f1 = fopen("test", "w+");
  1007.     if (f1 == NULL) {
  1008.     error("fopen error 13\n");
  1009.     }
  1010.     CheckFile("test", "", "fopen error 14\n");
  1011.     if (fputs("Again", f1) == EOF) {
  1012.     error("fopen error 15\n");
  1013.     }
  1014.     fclose(f1);
  1015.     f1 = fopen("test", "a+b");
  1016.     if (f1 == NULL) {
  1017.     error("fopen error 16\n");
  1018.     }
  1019.     fputs(" written", f1);
  1020.     fflush(f1);
  1021.     CheckFile("test", "Again written", "fopen error 17\n");
  1022.     fseek(f1, 0, SEEK_SET);
  1023.     if (getc(f1) != 'A') {
  1024.     error("fopen error 18\n");
  1025.     }
  1026.     fseek(f1, 0, SEEK_CUR);
  1027.     fputs(" to\n", f1);
  1028.     fclose(f1);
  1029.     CheckFile("test", "A to\n written", "fopen error 19\n");
  1030.     f1 = fopen("test", "w");
  1031.     fputs("Again written to\n", f1);
  1032.     fclose(f1);
  1033.     if (fopen("test", "q") != NULL) {
  1034.     error("fopen error 20\n");
  1035.     }
  1036.     if (fopen("test", "ba") != NULL) {
  1037.     error("fopen error 21\n");
  1038.     }
  1039.  
  1040.     /*
  1041.      * fdopen, freopen
  1042.      */
  1043.  
  1044.     fd = open("test", O_RDONLY, 0666);
  1045.     if (fd == -1) {
  1046.     error("fdopen error 1\n");
  1047.     }
  1048.     f1 = fdopen(fd, "r");
  1049.     if (fgets(buf, 100, f1) == NULL) {
  1050.     error("fdopen error 2\n");
  1051.     }
  1052.     if (strcmp(buf, "Again written to\n") != 0) {
  1053.     error("fdopen error 3\n");
  1054.     }
  1055.     /*
  1056.      * Tricky stuff:  it must be possible to have two FILE's open
  1057.      * simultaneously on the same stream id.  Sounds crazy, but some
  1058.      * UNIX programs depend on it.
  1059.      */
  1060.     f2 = fopen("test2", "w");
  1061.     f3 = fdopen(fileno(f2), "w");
  1062.     if (fputs("Test1", f2) == NULL) {
  1063.     error("fdopen error 4\n");
  1064.     }
  1065.     if (fputs("Test2", f3) == NULL) {
  1066.     error("fdopen error 5\n");
  1067.     }
  1068.     if (fputs(" and test3", f2) == NULL) {
  1069.     error("fdopen error 6\n");
  1070.     }
  1071.     if (fputs(" and test4", f3) == NULL) {
  1072.     error("fdopen error 7\n");
  1073.     }
  1074.     fflush(f2);
  1075.     fflush(f3);
  1076.     CheckFile("test2", "Test1 and test3Test2 and test4", "fdopen error 8\n");
  1077.     fclose(f2);
  1078.     fclose(f3);
  1079.     /*
  1080.      * More tricky stuff:  fdopen must seek to the end of the file in
  1081.      * "a" mode:  some UNIX programs depend on it.
  1082.      */
  1083.     fd = open("test2", O_WRONLY, 0666);
  1084.     if (fd == -1) {
  1085.     error("fdopen error 9\n");
  1086.     }
  1087.     f2 = fdopen(fd, "a");
  1088.     fputs(" and more", f2);
  1089.     fclose(f2);
  1090.     CheckFile("test2", "Test1 and test3Test2 and test4 and more",
  1091.         "fdopen error 10\n");
  1092.  
  1093.     f1 = freopen("test2", "w", f1);
  1094.     if (f1 == NULL) {
  1095.     error("freopen error 1\n");
  1096.     }
  1097.     if (fputs("Output data\n", f1) == EOF) {
  1098.     error("freopen error 2\n");
  1099.     }
  1100.     f1 = freopen("test2", "r", f1);
  1101.     if (f1 == NULL) {
  1102.     error("freopen error 3\n");
  1103.     }
  1104.     if (fgets(buf, 100, f1) == NULL) {
  1105.     error("freopen error 4\n");
  1106.     }
  1107.     if (strcmp(buf, "Output data\n") != 0) {
  1108.     error("freopen error 5\n");
  1109.     }
  1110.     CheckFile("test2", "Output data\n", "freopen error 6\n");
  1111.     fclose(f1);
  1112.  
  1113.     /*
  1114.      * feof, ferror, clearerr
  1115.      */
  1116.  
  1117.     f1 = fopen("test", "r");
  1118.     f2 = fopen("test", "w");
  1119.     if ((f1 == NULL) || (f2 == NULL)) {
  1120.     error("ferror error 1\n");
  1121.     }
  1122.     if (getc(f1) != EOF) {
  1123.     error("ferror error 2\n");
  1124.     }
  1125.     if (!feof(f1)) {
  1126.     error("ferror error 3\n");
  1127.     }
  1128.     putc('a', f2);
  1129.     fclose(f2);
  1130.     if (getc(f1) != EOF) {
  1131.     error("ferror error 4\n");
  1132.     }
  1133.     if (!feof(f1)) {
  1134.     error("ferror error 5\n");
  1135.     }
  1136.     clearerr(f1);
  1137.     if (feof(f1)) {
  1138.     error("ferror error 6\n");
  1139.     }
  1140.     if (getc(f1) != 'a') {
  1141.     error("ferror error 7\n");
  1142.     }
  1143.     if (feof(f1)) {
  1144.     error("ferror error 8\n");
  1145.     }
  1146.     if (getc(f1) != EOF) {
  1147.     error("ferror error 9\n");
  1148.     }
  1149.     if (!feof(f1)) {
  1150.     error("ferror error 10\n");
  1151.     }
  1152.     fclose(f1);
  1153.     fd = open("test", O_RDONLY, 0666);
  1154.     if (fd == -1) {
  1155.     error("ferror error 11\n");
  1156.     }
  1157.     f1 = fdopen(fd, "r+");
  1158.     setvbuf(f1, 0, _IONBF, 1);
  1159.     if (putc('x', f1) != EOF) {
  1160.     error("ferror error 12\n");
  1161.     }
  1162.     if (ferror(f1) == 0) {
  1163.     error("ferror error 13\n");
  1164.     }
  1165.     fseek(f1, 0, 0);
  1166.     if (getc(f1) != EOF) {
  1167.     error("ferror error 14\n");
  1168.     }
  1169.     if (ferror(f1) == 0) {
  1170.     error("ferror error 15\n");
  1171.     }
  1172.     clearerr(f1);
  1173.     if (ferror(f1) != 0) {
  1174.     error("ferror error 16\n");
  1175.     }
  1176.     if (getc(f1) != 'a') {
  1177.     error("ferror error 17\n");
  1178.     }
  1179.     fclose(f1);
  1180.  
  1181.     /*
  1182.      * _cleanup
  1183.      */
  1184.  
  1185.     if (fork() == 0) {
  1186.     f1 = fopen("test", "w");
  1187.     if (f1 == NULL) {
  1188.         error("_cleanup error 1\n");
  1189.     }
  1190.     fputs("Test string", f1);
  1191.     exit(0);
  1192.     }
  1193.     wait(&status);
  1194.     if (status.w_T.w_Retcode == 0) {
  1195.     CheckFile("test", "Test string", "_cleanup error 2\n");
  1196.     }
  1197.  
  1198.     /*
  1199.      * fprintf
  1200.      */
  1201.  
  1202.     f1 = fopen("test", "w");
  1203.     f2 = fopen("test", "r");
  1204.     if ((f1 == NULL) || (f2 == NULL)) {
  1205.     error("sprintf error 1\n");
  1206.     }
  1207.     setvbuf(f1, 0, _IOLBF, BUFSIZ);
  1208.     length = fprintf(f1, "%*d %d %d %d\n", 6, 34, 16923, -12, -1);
  1209.     CheckNext(f2, "    34 16923 -12 -1\n", length, "fprintf error 2\n");
  1210.     length = fprintf(f1, "%4d %4d %4d %4d %d %#x %#X\n", 6, 34,
  1211.         16923, -12, -1, 0, 0);
  1212.  
  1213.     CheckNext(f2, "   6   34 16923  -12 -1 0 0\n", length,
  1214.         "fprintf error 3\n");
  1215.  
  1216.     length = fprintf(f1, "%4u %4u %4u %4u %d %#o\n", 6, 34, 16923, -12, -1, 0);
  1217.     CheckNext(f2, "   6   34 16923 4294967284 -1 0\n", length,
  1218.         "fprintf error 4\n");
  1219.  
  1220.     length = fprintf(f1, "%-4d %-4d %-4d %-4ld\n", 6, 34, 16923, -12, -1);
  1221.     CheckNext(f2, "6    34   16923 -12 \n", length,
  1222.         "fprintf error 5\n");
  1223.  
  1224.     length = fprintf(f1, "%04d %04d %04d %04d\n", 6, 34, 16923, -12, -1);
  1225.     CheckNext(f2, "0006 0034 16923 -012\n", length,
  1226.         "fprintf error 6\n");
  1227.  
  1228.     length = fprintf(f1, "%00*d\n", 6, 34, 16923, -12, -1);
  1229.     CheckNext(f2, "000034\n", length, "fprintf error 7\n");
  1230.  
  1231.     length = fprintf(f1, "%4x %4x %4x %4x\n", 6, 34, 16923, -12, -1);
  1232.     CheckNext(f2, "   6   22 421b fffffff4\n", length,
  1233.         "fprintf error 8\n");
  1234.  
  1235.     length = fprintf(f1, "%#x %#X %#X %#x\n", 6, 34, 16923, -12, -1);
  1236.     CheckNext(f2, "0x6 0X22 0X421B 0xfffffff4\n", length,
  1237.         "fprintf error 9\n");
  1238.  
  1239.     length = fprintf(f1, "%#20x %#20x %#20x %#20x\n", 6, 34, 16923, -12, -1);
  1240.     CheckNext(f2, "                 0x6                 0x22               0x421b           0xfffffff4\n", length, "fprintf error 10\n");
  1241.  
  1242.     length = fprintf(f1, "%-#20x %-#20x %-#20x %-#20x\n", 6, 34, 16923, -12, -1);
  1243.     CheckNext(f2, "0x6                  0x22                 0x421b               0xfffffff4          \n", length, "fprintf error 11\n");
  1244.  
  1245.     length = fprintf(f1, "%-#20o %#-20o %#-20o %#-20o\n", 6, 34, 16923, -12, -1);
  1246.     CheckNext(f2, "06                   042                  041033               037777777764        \n", length, "fprintf error 12\n");
  1247.  
  1248.     length = fprintf(f1, "%s %s %c %s\n", "abcd",
  1249.         "This is a very long test string.", 'x', "x");
  1250.     CheckNext(f2, "abcd This is a very long test string. x x\n", length,
  1251.         "fprintf error 13\n");
  1252.  
  1253.     length = fprintf(f1, "%20s %20s %20c %20s\n", "abcd",
  1254.         "This is a very long test string.", 'x', "x");
  1255.     CheckNext(f2, "                abcd This is a very long test string.                    x                    x\n", length, "fprintf error 14\n");
  1256.  
  1257.     length = fprintf(f1, "%.10s %.10s %c %.10s\n", "abcd",
  1258.         "This is a very long test string.", 'x', "x");
  1259.     CheckNext(f2, "abcd This is a  x x\n", length, "fprintf error 15\n");
  1260.  
  1261.     length = fprintf(f1, "%s %s %1 %% %c %s\n", "abcd",
  1262.         "This is a very long test string.", 'x', "x");
  1263.     CheckNext(f2, "abcd This is a very long test string.  % x x\n", length,
  1264.         "fprintf error 16\n");
  1265.  
  1266.     length = fprintf(f1, "%e %e %e %e\n", 34.2e102, 16923.0/247,
  1267.         -.125, -16000., .000053);
  1268.     CheckNext(f2, "3.420000e+103 6.851417e+01 -1.250000e-01 -1.600000e+04\n", length, "fprintf error 17\n");
  1269.  
  1270.     length = fprintf(f1, "%20e %20e %20e %20e\n", 34.2e102, 16923.0/247,
  1271.         -.125, -16000., .000053);
  1272.     CheckNext(f2, "       3.420000e+103         6.851417e+01        -1.250000e-01        -1.600000e+04\n", length, "fprintf error 18\n");
  1273.  
  1274.     length = fprintf(f1, "%.1e %.1e %.1e %.1e\n", 34.2e102, 16923.0/247,
  1275.         -.125, -16000., .000053);
  1276.     CheckNext(f2, "3.4e+103 6.9e+01 -1.3e-01 -1.6e+04\n", length,
  1277.         "fprintf error 19\n");
  1278.  
  1279.     length = fprintf(f1, "%020e %020e %020e %020e\n", 34.2e102, 16923.0/247,
  1280.         -.125, -16000., .000053);
  1281.     CheckNext(f2, "00000003.420000e+103 000000006.851417e+01 -00000001.250000e-01 -00000001.600000e+04\n", length, "fprintf error 20\n");
  1282.  
  1283.     length = fprintf(f1, "%7.1e %7.1e %7.1e %7.1e\n", 34.2e102, 16923.0/247,
  1284.         -.125, -16000., .000053);
  1285.     CheckNext(f2, "3.4e+103 6.9e+01 -1.3e-01 -1.6e+04\n", length,
  1286.         "fprintf error 21\n");
  1287.  
  1288.     length = fprintf(f1, "%f %f %f %f\n", 34.2e102, 16923.0/247,
  1289.         -.125, -16000., .000053);
  1290.     CheckNext(f2, "34200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000 68.514170 -0.125000 -16000.000000\n", length, "fprintf error 22\n");
  1291.  
  1292.     length = fprintf(f1, "%.4f %.4f %.4f %.4f %.4f\n", 34.2e102, 16923.0/247,
  1293.         -.125, -16000., .000053);
  1294.     CheckNext(f2, "34200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0000 68.5142 -0.1250 -16000.0000 0.0001\n", length, "fprintf error 23\n");
  1295.  
  1296.     length = fprintf(f1, "%.4e %.5e %.6e\n", -9.99995, -9.99995, 9.99995);
  1297.     CheckNext(f2, "-1.0000e+01 -9.99995e+00 9.999950e+00\n", length,
  1298.         "fprintf error 24\n");
  1299.  
  1300.     length = fprintf(f1, "%.4f %.5f %.6f\n", -9.99995, -9.99995, 9.99995);
  1301.     CheckNext(f2, "-10.0000 -9.99995 9.999950\n", length,
  1302.         "fprintf error 25\n");
  1303.  
  1304.     length = fprintf(f1, "%20f %-20f %020f\n", -9.99995, -9.99995, 9.99995);
  1305.     CheckNext(f2, "           -9.999950 -9.999950            0000000000009.999950\n", length, "fprintf error 26\n");
  1306.  
  1307.     length = fprintf(f1, "%-020f %020f\n", -9.99995, -9.99995, 9.99995);
  1308.     CheckNext(f2, "-9.999950            -000000000009.999950\n", length,
  1309.         "fprintf error 27\n");
  1310.  
  1311.     length = fprintf(f1, "%.0e %#.0e\n", -9.99995, -9.99995, 9.99995);
  1312.     CheckNext(f2, "-1e+01 -1.e+01\n", length, "fprintf error 28\n");
  1313.  
  1314.     length = fprintf(f1, "%.0f %#.0f\n", -9.99995, -9.99995, 9.99995);
  1315.     CheckNext(f2, "-10 -10.\n", length, "fprintf error 29\n");
  1316.  
  1317.     length = fprintf(f1, "%.4f %.5f %.6f\n", -9.99995, -9.99995, 9.99995);
  1318.     CheckNext(f2, "-10.0000 -9.99995 9.999950\n", length,
  1319.         "fprintf error 30\n");
  1320.  
  1321.     length = fprintf(f1, "%.3g\n", 12341.0);
  1322.     CheckNext(f2, "1.23e+04\n", length, "fprintf error 31\n");
  1323.  
  1324.     length = fprintf(f1, "%.3G\n", 1234.12345);
  1325.     CheckNext(f2, "1.23E+03\n", length, "fprintf error 32\n");
  1326.  
  1327.     length = fprintf(f1, "%.3g\n", 123.412345);
  1328.     CheckNext(f2, "123\n", length, "fprintf error 33\n");
  1329.  
  1330.     length = fprintf(f1, "%.3g\n", 12.3412345);
  1331.     CheckNext(f2, "12.3\n", length, "fprintf error 34\n");
  1332.  
  1333.     length = fprintf(f1, "%.3g\n", 1.23412345);
  1334.     CheckNext(f2, "1.23\n", length, "fprintf error 35\n");
  1335.  
  1336.     length = fprintf(f1, "%.3g\n", .123412345);
  1337.     CheckNext(f2, "0.123\n", length, "fprintf error 36\n");
  1338.  
  1339.     length = fprintf(f1, "%.3g\n", .012341);
  1340.     CheckNext(f2, "0.0123\n", length, "fprintf error 37\n");
  1341.  
  1342.     length = fprintf(f1, "%.3g\n", .0012341);
  1343.     CheckNext(f2, "0.00123\n", length, "fprintf error 38\n");
  1344.  
  1345.     length = fprintf(f1, "%.3g\n", .00012341);
  1346.     CheckNext(f2, "0.000123\n", length, "fprintf error 39\n");
  1347.  
  1348.     length = fprintf(f1, "%.3g\n", .00001234);
  1349.     CheckNext(f2, "1.23e-05\n", length, "fprintf error 40\n");
  1350.  
  1351.     length = fprintf(f1, "%.4g\n", 9999.5);
  1352.     CheckNext(f2, "1e+04\n", length, "fprintf error 41\n");
  1353.  
  1354.     length = fprintf(f1, "%.4g\n", 999.95);
  1355.     CheckNext(f2, "1000\n", length, "fprintf error 42\n");
  1356.  
  1357.     length = fprintf(f1, "%.3g\n", 1.0);
  1358.     CheckNext(f2, "1\n", length, "fprintf error 43\n");
  1359.  
  1360.     length = fprintf(f1, "%.\n", 1.0);
  1361.     CheckNext(f2, "\n", length, "fprintf error 44\n");
  1362.  
  1363.     length = fprintf(f1, "%.3g\n", .1);
  1364.     CheckNext(f2, "0.1\n", length, "fprintf error 45\n");
  1365.  
  1366.     length = fprintf(f1, "%.3g\n", .01);
  1367.     CheckNext(f2, "0.01\n", length, "fprintf error 46\n");
  1368.  
  1369.     length = fprintf(f1, "%.3g\n", .001);
  1370.     CheckNext(f2, "0.001\n", length, "fprintf error 47\n");
  1371.  
  1372.     length = fprintf(f1, "%.3g\n", .0001);
  1373.     CheckNext(f2, "1e-04\n", length, "fprintf error 48\n");
  1374.  
  1375.     length = fprintf(f1, "%.3g\n", .00001);
  1376.     CheckNext(f2, "1e-05\n", length, "fprintf error 49\n");
  1377.  
  1378.     length = fprintf(f1, "%#.3g\n", 1234.0);
  1379.     CheckNext(f2, "1.23e+03\n", length, "fprintf error 50\n");
  1380.  
  1381.     length = fprintf(f1, "%#.3G\n", 9999.5);
  1382.     CheckNext(f2, "1.00E+04\n", length, "fprintf error 51\n");
  1383.  
  1384.     length = fprintf(f1, "%e %f %g\n", 0.0, 0.0, 0.0, 0.0);
  1385.     CheckNext(f2, "0.000000e+00 0.000000 0\n", length, "fprintf error 52\n");
  1386.  
  1387.     length = fprintf(f1, "%.4e %.4f %.4g\n", 0.0, 0.0, 0.0, 0.0);
  1388.     CheckNext(f2, "0.0000e+00 0.0000 0\n", length, "fprintf error 53\n");
  1389.  
  1390.     length = fprintf(f1, "%#.4e %#.4f %#.4g\n", 0.0, 0.0, 0.0, 0.0);
  1391.     CheckNext(f2, "0.0000e+00 0.0000 0.000\n", length, "fprintf error 54\n");
  1392.  
  1393.     length = fprintf(f1, "%.0e %.0f %.0g\n", 0.0, 0.0, 0.0, 0.0);
  1394.     CheckNext(f2, "0e+00 0 0\n", length, "fprintf error 55\n");
  1395.  
  1396.     length = fprintf(f1, "%#.0e %#.0f %#.0g\n", 0.0, 0.0, 0.0, 0.0);
  1397.     CheckNext(f2, "0.e+00 0. 0.\n", length, "fprintf error 56\n");
  1398.  
  1399.     length = fprintf(f1, "%3.0f %3.0f %3.0f %3.0f\n", 0.0, 0.1, 0.01, 0.001);
  1400.     CheckNext(f2, "  0   0   0   0\n", length, "fprintf error 57\n");
  1401.  
  1402.     length = fprintf(f1, "%3.0f %3.0f %3.0f %3.0f\n", 1.0, 1.1, 1.01, 1.001);
  1403.     CheckNext(f2, "  1   1   1   1\n", length, "fprintf error 58\n");
  1404.  
  1405.     length = fprintf(f1, "%3.1f %3.1f %3.1f %3.1f\n", 0.0, 0.1, 0.01, 0.001);
  1406.     CheckNext(f2, "0.0 0.1 0.0 0.0\n", length, "fprintf error 59\n");
  1407.  
  1408.     fclose(f1);
  1409.     fclose(f2);
  1410.  
  1411.     /*
  1412.      * sscanf
  1413.      */
  1414.  
  1415.     count = sscanf("-20 1476 \n33 0", "%d %d %d %d", &d1, &d2, &d3, &d4);
  1416.     if ((count != 4) || (d1 != -20) || (d2 != 1476) || (d3 != 33)
  1417.         || (d4 != 0)) {
  1418.     error("sscanf error 1\n");
  1419.     }
  1420.  
  1421.     d4 = -1;
  1422.     count = sscanf("-45 16 7890 +10", "%2d %*d %10d %d", &d1, &d2, &d3, &d4);
  1423.     if ((count != 3) || (d1 != -4) || (d2 != 16) || (d3 != 7890)
  1424.         || (d4 != -1)) {
  1425.     error("sscanf error 2\n");
  1426.     }
  1427.  
  1428.     count = sscanf("-45 16 +10 987", "%D %  %hD %d", &d1, &d2, &d3, &d4);
  1429.     if ((count != 4) || (d1 != -45) || (d2 != 16) || (d3 != 10)
  1430.         || (d4 != 987)) {
  1431.     error("sscanf error 3\n");
  1432.     }
  1433.  
  1434.     sa1[0] = sa1[1] = sa1[2] = sa2[0] = sa2[1] = sa2[2] = -1;
  1435.     count = sscanf("14 1ab 62 10", "%hd %x %O %hx", &sa1[1], &d2, &d3,
  1436.         &sa2[1]);
  1437.     if ((count != 4) || (sa1[0] != -1) || (sa1[1] != 14) || (sa1[2] != -1)
  1438.         || (d2 != 427) || (d3 != 50) || (sa2[0] != -1) || (sa2[1] != 0x10)
  1439.         || (sa2[2] != -1)) {
  1440.     error("sscanf error 4\n");
  1441.     }
  1442.  
  1443.     count = sscanf("12345670 1234567890ab cdefg", "%o     %o %x %X",
  1444.         &d1, &d2, &d3, &d4);
  1445.     if ((count != 4) || (d1 != 2739128) || (d2 != 342391) || (d3 != 561323)
  1446.         || (d4 != 52719)) {
  1447.     error("sscanf error 5\n");
  1448.     }
  1449.  
  1450.     count = sscanf("ab123-24642", "%2x %3hx %3o %2ho", &d1, &s1, &d3, &s2);
  1451.     if ((count != 4) || (d1 != 171) || (s1 != 0x123) || (d3 != -20)
  1452.         || (s2 != 0x34)) {
  1453.     error("sscanf error 6\n");
  1454.     }
  1455.  
  1456.     d2 = d3 = d4 = -1;
  1457.     count = sscanf("42 43", "%d %", &d1, &d2, &d3, &d4);
  1458.     if ((count != -1) || (d1 != 42) || (d2 != -1) || (d3 != -1)
  1459.         || (d4 != -1)) {
  1460.     error("sscanf error 7\n");
  1461.     }
  1462.  
  1463.     d2 = d3 = d4 = -1;
  1464.     count = sscanf("42", "%d", &d1, &d2, &d3, &d4);
  1465.     if ((count != 1) || (d1 != 42) || (d2 != -1) || (d3 != -1)
  1466.         || (d4 != -1)) {
  1467.     error("sscanf error 8\n");
  1468.     }
  1469.  
  1470.     d2 = d3 = d4 = -1;
  1471.     count = sscanf("1 2 3 4 5", "%q %r", &d1, &d2, &d3, &d4);
  1472.     if ((count != 2) || (d1 != 1) || (d2 != 2) || (d3 != -1)
  1473.         || (d4 != -1)) {
  1474.     error("sscanf error 9\n");
  1475.     }
  1476.  
  1477.     d3 = d4 = -1;
  1478.     count = sscanf("1234567 234 567  ", "%*3x %x %*o %4o", &d1, &d2, &d3, &d4);
  1479.     if ((count != 2) || (d1 != 17767) || (d2 != 375) || (d3 != -1)
  1480.         || (d4 != -1)) {
  1481.     error("sscanf error 10\n");
  1482.     }
  1483.  
  1484.     d3 = d4 = -1;
  1485.     count = sscanf("1234567 234 567  ", "%*3x %x %*o %4o", &d1, &d2, &d3, &d4);
  1486.     if ((count != 2) || (d1 != 17767) || (d2 != 375) || (d3 != -1)
  1487.         || (d4 != -1)) {
  1488.     error("sscanf error 11\n");
  1489.     }
  1490.  
  1491.     d1 = d2 = d3 = d4 = -1;
  1492.     count = sscanf("a    1234", "%d %d", &d1, &d2, &d3, &d4);
  1493.     if ((count != 0) || (d1 != -1) || (d2 != -1) || (d3 != -1)
  1494.         || (d4 != -1)) {
  1495.     error("sscanf error 11\n");
  1496.     }
  1497.  
  1498.     count = sscanf("12345678", "%2d %2d %2d %2d", &d1, &d2, &d3, &d4);
  1499.     if ((count != 4) || (d1 != 12) || (d2 != 34) || (d3 != 56)
  1500.         || (d4 != 78)) {
  1501.     error("sscanf error 12\n");
  1502.     }
  1503.  
  1504.     d3 = d4 = -1;
  1505.     count = sscanf("1 2 ", "%d %d %d %d", &d1, &d2, &d3, &d4);
  1506.     if ((count != 2) || (d1 != 1) || (d2 != 2) || (d3 != -1)
  1507.         || (d4 != -1)) {
  1508.     error("sscanf error 13\n");
  1509.     }
  1510.  
  1511.     d1 = d2 = d3 = d4 = -1;
  1512.     count = sscanf("  a", " a%d %d %d %d", &d1, &d2, &d3, &d4);
  1513.     if ((count != -1) || (d1 != -1) || (d2 != -1) || (d3 != -1)
  1514.         || (d4 != -1)) {
  1515.     error("sscanf error 14\n");
  1516.     }
  1517.  
  1518.     fp4 = -1.0;
  1519.     count = sscanf("2.1 -3.0e8 .99962 a", "%f%f%f%f", &fp1, &fp2, &fp3, &fp4);
  1520.     CheckFp(count, 3, fp1, fp2, fp3, fp4,
  1521.         "2.100000e+00 -3.000000e+08 9.996200e-01 -1.000000e+00",
  1522.         "sscanf error 15\n");
  1523.  
  1524.     count = sscanf("-1.2345 +8.2 9", "%3e %3f %f %f", &fp1, &fp2, &fp3, &fp4);
  1525.     CheckFp(count, 4, fp1, fp2, fp3, fp4,
  1526.         "-1.000000e+00 2.340000e+02 5.000000e+00 8.200000e+00",
  1527.         "sscanf error 16\n");
  1528.  
  1529.     fp4 = -1.0;
  1530.     count = sscanf("1e00004 332E-4 3e+4", "%f %*2e %f %f", &fp1,
  1531.         &fp2, &fp3, &fp4);
  1532.     CheckFp(count, 3, fp1, fp2, fp3, fp4,
  1533.         "1.000000e+04 2.000000e-04 3.000000e+04 -1.000000e+00",
  1534.         "sscanf error 17\n");
  1535.  
  1536.     fp4 = -1.0;
  1537.     count = sscanf("1. 47.6 2.e2 3.e-", "%f %*f %f %f", &fp1, &fp2,
  1538.         &fp3, &fp4);
  1539.     CheckFp(count, 3, fp1, fp2, fp3, fp4,
  1540.         "1.000000e+00 2.000000e+02 3.000000e+00 -1.000000e+00",
  1541.         "sscanf error 18\n");
  1542.  
  1543.     fp3 = fp4 = -1.0;
  1544.     count = sscanf("1.eabc", "%f %x", &fp1, &fp2, &fp3, &fp4);
  1545.     CheckFp(count, 2, fp1, fp2, fp3, fp4,
  1546.         "1.000000e+00 3.850768e-42 -1.000000e+00 -1.000000e+00",
  1547.         "sscanf error 19\n");
  1548.  
  1549.     count = sscanf("4.6 99999.7 876.43e-1 118", "%f %f %f %e",
  1550.         &fp1, &fp2, &fp3, &fp4);
  1551.     CheckFp(count, 4, fp1, fp2, fp3, fp4,
  1552.         "4.600000e+00 9.999970e+04 8.764300e+01 1.180000e+02",
  1553.         "sscanf error 20\n");
  1554.  
  1555.     count = sscanf("1.2345 697.0e-3 124 .00005", "%F %E %lf %le",
  1556.         &dbl1, &dbl2, &dbl3, &dbl4);
  1557.     CheckFp(count, 4, dbl1, dbl2, dbl3, dbl4,
  1558.         "1.234500e+00 6.970000e-01 1.240000e+02 5.000000e-05",
  1559.         "sscanf error 21\n");
  1560.  
  1561.     count = sscanf("4.6 99999.7 876.43e-1 118", "%F %F %F %F",
  1562.         &dbl1, &dbl2, &dbl3, &dbl4);
  1563.     CheckFp(count, 4, dbl1, dbl2, dbl3, dbl4,
  1564.         "4.600000e+00 9.999970e+04 8.764300e+01 1.180000e+02",
  1565.         "sscanf error 22\n");
  1566.  
  1567.     dbl2 = dbl3 = dbl4 = -1.0;
  1568.     count = sscanf("4.6abc", "%F %F %f %f",
  1569.         &dbl1, &dbl2, &dbl3, &dbl4);
  1570.     CheckFp(count, 1, dbl1, dbl2, dbl3, dbl4,
  1571.         "4.600000e+00 -1.000000e+00 -1.000000e+00 -1.000000e+00",
  1572.         "sscanf error 23\n");
  1573.  
  1574.     dbl3 = dbl4 = -1.0;
  1575.     count = sscanf("4.6 5.2", "%F %F %F %F",
  1576.         &dbl1, &dbl2, &dbl3, &dbl4);
  1577.     CheckFp(count, 2, dbl1, dbl2, dbl3, dbl4,
  1578.         "4.600000e+00 5.200000e+00 -1.000000e+00 -1.000000e+00",
  1579.         "sscanf error 24\n");
  1580.  
  1581.     CheckString("abc defghijk dum ", "%s %3s %20s %s", 4,
  1582.         "abc", "def", "ghijk", "dum",
  1583.         "sscanf error 25\n");
  1584.  
  1585.     CheckString("abc       bcdef", "%5c%c%1s %s", 4,
  1586.         "abc  ", " XXXX", "b", "cdef",
  1587.         "sscanf error 26\n");
  1588.  
  1589.     CheckString("123456 test ", "%*c%*s %s %s %s", 1,
  1590.         "test", "XXXXX", "XXXXX", "XXXXX",
  1591.         "sscanf error 27\n");
  1592.  
  1593.     CheckString("ababcd01234  f 123450", "%4[abcd] %4[abcd] %[^abcdef] %[^0]",
  1594.         4, "abab", "cd", "01234  ", "f 12345",
  1595.         "sscanf error 28\n");
  1596.  
  1597.     CheckString("aaaaaabc aaabcdefg  + +  XYZQR",
  1598.         "%*4[a] %s %*4[a]%s%*4[ +]%10c", 3,
  1599.         "aabc", "bcdefg", "+  XYZQR", "XXXXX",
  1600.         "sscanf error 29\n");
  1601.  
  1602.     /*
  1603.      * printf, scanf, fscanf, sprintf, vprintf, vsprintf
  1604.      */
  1605.  
  1606.     fd = dup(1);
  1607.     fclose(stdout);
  1608.     f1 = fopen("test", "w");
  1609.     count = printf("%d and %d", 47, 102);
  1610.     if (count != 10) {
  1611.     error("printf error 1\n");
  1612.     }
  1613.     count = CheckVprintf("  %x then %x", 15, 65);
  1614.     if (count != 11) {
  1615.     error("printf error 2\n");
  1616.     }
  1617.     fclose(stdout);
  1618.     CheckFile("test", "47 and 102  f then 41", "printf error 3\n");
  1619.     dup2(fd, 1);
  1620.     (void) fdopen(1, "w");
  1621.     close(fd);
  1622.  
  1623.     fd = dup(0);
  1624.     fclose(stdin);
  1625.     f1 = fopen("test", "r");
  1626.     count = scanf("%d and %d %d", &d1, &d2, &d3);
  1627.     if ((count != 2) || (d1 != 47) || (d2 != 102)) {
  1628.     error("scanf error 1\n");
  1629.     }
  1630.     fclose(stdin);
  1631.     dup2(fd, 0);
  1632.     (void) fdopen(0, "r");
  1633.     close(fd);
  1634.  
  1635.     f1 = fopen("test", "r");
  1636.     count = fscanf(f1, "%d and %d %d", &d1, &d2, &d3);
  1637.     if ((count != 2) || (d1 != 47) || (d2 != 102)) {
  1638.     error("fscanf error 1\n");
  1639.     }
  1640.     fclose(f1);
  1641.  
  1642.     if (sprintf(buf, "%s %d", "The value is", 19) != buf) {
  1643.     error("sprintf error 1\n");
  1644.     }
  1645.     if (strcmp(buf, "The value is 19") != 0) {
  1646.     error("sprintf error 2\n");
  1647.     }
  1648.  
  1649.     if (CheckVsprintf(buf, "%s %d", "The value is", 19) != buf) {
  1650.     error("vsprintf error 1\n");
  1651.     }
  1652.     if (strcmp(buf, "The value is 19") != 0) {
  1653.     error("vsprintf error 2\n");
  1654.     }
  1655. }
  1656.